<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>3小时才能点击一次按钮 - localStorage</title>
</head>

<body>
    <button type="button" id="myButton">点击下试试</button>
    <script>
        const myButton = document.getElementById('myButton');
        myButton.addEventListener('click', () => {
            //有时间则为存储时间，没有则为0
            let lastClickTime = parseInt(localStorage.getItem('lastClickTime')) || '0';
            //拿到当前时间
            const now = Date.now();
            if (now - lastClickTime < 3 * 60 * 60 * 1000) {
                // 如果与上次点击时间相差不到3小时，则不算一次有效点击
                console.log("等等吧")
                return;
            }

            // 更新lastClickTime并写入localStorage
            localStorage.setItem('lastClickTime', now)

            // 弹窗提示
            alert('点击成功');
        });




    </script>

</body>

</html>